﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class morning_EditStockInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            bd();
        }
    }

    void bd()
    {
        string id = Request["id"];
        int Id = int.Parse(id);
        TemporaryModel.TemporaryEntities dal = DAL.DBManager.GetTempEmtity();
        TemporaryModel.StockBusinessInfo sbi = dal.StockBusinessInfo.First(x => x.id == Id);
        lblid.Text = sbi.id.ToString();
        txtUserName.Text = sbi.userName;
        this.stime.Value = ((DateTime)sbi.wtDate).ToString("yyyy-MM-dd");
        txtCode.Text = sbi.stockCode;
        txtName.Text = sbi.stockName;
        txtPrice.Text=sbi.wtPrice.ToString();
        txtNum.Text = sbi.num.ToString();
        if (sbi.buyOrSell == "买入")
            rbtnBuy.Checked = true;
        else
            rbtnSell.Checked = true;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        TemporaryModel.TemporaryEntities dal = DAL.DBManager.GetTempEmtity();
        int id = int.Parse(lblid.Text);
        TemporaryModel.StockBusinessInfo sbi = dal.StockBusinessInfo.First(x => x.id == id);
        try
        {
            sbi.userName = txtUserName.Text;
            sbi.wtDate = DateTime.Parse(stime.Value);
            sbi.stockCode = txtCode.Text;
            sbi.stockName = txtName.Text;
            sbi.wtPrice = decimal.Parse(txtPrice.Text);
            sbi.num = int.Parse(txtNum.Text);
            if (rbtnBuy.Checked)
                sbi.buyOrSell = "买入";
            else
                sbi.buyOrSell = "卖出";
            sbi.Idate = DateTime.Now;
            try
            {
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "save", "alert('修改成功'),document.location='StockInfo.aspx'", true);
            }
            catch (Exception ex)
            {
                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "editStockBusinessInfo", LogYuMin.LogStates.ERROR, ex);
                ClientScript.RegisterStartupScript(GetType(), "save", "alert('修改失败')", true);
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "save", "alert('录入数据格式有误！请检查数据格式是否正确')", true);

        }
    }
}